/*
 * @(#)TestResults.java
 *
 *
 *
 * ver 1.0 Aug 16, 2007 ggranum
 */
package org.testfire.model;

import java.util.Date;
import org.testfire.translator.TranslatorFactory;
import org.testfire.util.DomUtil;
import org.w3c.dom.Element;

public class TestResultType implements TestFireObject
{
    private TestType parent;

    public static final String PASSED = "passed";

    public static final String FAILED = "failed";

    public static final String IGNORED = "ignored";

    private final Element element;

    public TestResultType( Element element )
    {
        this.element = element;
    }

    public TestResultType( TestType parent )
    {
        element = parent.getElement().getOwnerDocument().createElement( "tf:TestResults" );
        this.parent = parent;
        parent.addTestResult( element );
    }

    public TestResultType( TestType parent, Element element )
    {
        this.parent = parent;
        this.element = element;
    }

    public void setParent( TestFireObject parent )
    {
        this.parent = (TestType)parent;
    }

    public TestFireObject getParent()
    {
        return parent;
    }

    /**
     * @todo Modify the interface to use generics, and just use getParent instead of creating this method.
     * @return The parent test.
     */
    public TestType getOwningTest(){
        return parent;
    }

    public Object translate( TestsetType testset )
    {
        return TranslatorFactory.translate( testset, this );
    }

    public String getDescription()
    {
        return DomUtil.getChildElementTextByTagName( "description", element );
    }

    public void setDescription( String description )
    {
        Element el = DomUtil.getChildElementByTagName( "description", element );
        if ( el == null )
        {
            el = element.getOwnerDocument().createElement( "tf:description" );
            element.insertBefore( el, element.getFirstChild() );
        }
        el.setTextContent( description );
    }

    public TestFailureType getFailure()
    {
        Element el = DomUtil.getChildElementByTagName( "tf:Failure", element );
        return el == null ? null : new TestFailureType( el );
    }

    public void setFailure( String message, String trace, String testHeader )
    {
        if ( DomUtil.getChildElementByTagName( "tf:Failure", element ) != null )
        {
            throw new IllegalStateException( "tf:Failure has already been set on this TestResult." );
        }
        Element failure = element.getOwnerDocument().createElement( "tf:Failure" );
        Element messageEl = element.getOwnerDocument().createElement( "tf:message" );
        Element traceEl = element.getOwnerDocument().createElement( "tf:trace" );

        messageEl.setTextContent( message );
        traceEl.setTextContent( trace );
        failure.appendChild( messageEl );
        failure.appendChild( traceEl );
        failure.setAttribute( "testHeader", testHeader );
        element.appendChild( failure );

    }

    public String getResult()
    {
        return element.getAttribute( "result" );
    }

    public void setResult( String result )
    {
        element.setAttribute( "result", result );
    }

    public Date getRunDate()
    {
        return DomUtil.toJavaDate( element.getAttribute( "runDate" ) );
    }

    public void setRunDate( Date runDate )
    {
        element.setAttribute( "runDate", DomUtil.toXMLDateTime( runDate ) );
    }

    public int getRunTime()
    {
        String s = element.getAttribute( "runTime" );
        return (s!=null && s.length() >0 )?  Integer.parseInt( s ) : 0;
    }

    public void setRunTime( int runTime )
    {
        element.setAttribute( "runTime", String.valueOf( runTime ) );
    }

    

    /**
     * Do not make static.
     */
    public class TestFailureType
    {
        String message;

        String trace;

        private String testHeader;

        private TestFailureType( Element element )
        {
            message = DomUtil.getChildElementTextByTagName( "tf:message", element );
            trace = DomUtil.getChildElementTextByTagName( "tf:trace", element );
            testHeader = element.getAttribute( "testHeader" );
        }

        public String getMessage()
        {
            return message;
        }

        public String getTrace()
        {
            return trace;
        }

        public String getJsSafeTrace()
        {
            return trace.replaceAll("\\r", "").replaceAll( "\\n","\\\\n") ;
        }

        public String getTestHeader()
        {
            return testHeader;
        }
    }
}

// eof: TestResults.java